package com.example.pwj.ffstudy.ui.zhihu.presenter;

import com.blankj.utilcode.util.LogUtils;
import com.example.pwj.ffstudy.base.BaseFragment;
import com.example.pwj.ffstudy.bean.zdaily.ZDailyNews;
import com.example.pwj.ffstudy.bean.zdaily.ZDailyNewsSection;
import com.example.pwj.ffstudy.bean.zdaily.ZDailyStory;
import com.example.pwj.ffstudy.ui.zhihu.contract.ZNewsContract;
import com.example.pwj.ffstudy.ui.zhihu.model.ZNewsModel;
import com.example.pwj.ffstudy.util.DateUtil;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by pwj on 2017/8/23.
 */

public class ZNewsPresenter implements ZNewsContract.Presenter {
    private ZNewsContract.View view;
    private ZNewsModel model;
    private int mCurrentPage = -1;//用before api -1是今天

    @Inject
    ZNewsPresenter(ZNewsModel model) {
        LogUtils.e("ZNewsPresenter");
        this.model = model;
    }

    @Override
    public void loadZDailyNews(final boolean isLoadMore) {
        String header = "";
        if (!isLoadMore) {
            mCurrentPage = -1;
            header = "今日新闻";
        } else {
            mCurrentPage++;
            header = DateUtil.getDateAndWeek(mCurrentPage + 1);
        }
        model.getZDailyNews(mCurrentPage)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .concatMap(new Function<ZDailyNews, ObservableSource<ZDailyStory>>() {
                    @Override
                    public ObservableSource<ZDailyStory> apply(@NonNull ZDailyNews zDailyNews) throws Exception {
                        return Observable.fromIterable(zDailyNews.getStories());
                    }
                })
                .map(new Function<ZDailyStory, ZDailyNewsSection>() {
                    @Override
                    public ZDailyNewsSection apply(@NonNull ZDailyStory zDailyStory) throws Exception {
                        return new ZDailyNewsSection(zDailyStory);
                    }
                })
                .startWith(new ZDailyNewsSection(true, header))
                .toList()
                .observeOn(AndroidSchedulers.mainThread())
                .compose(((BaseFragment) view).<List<ZDailyNewsSection>>bindToLifecycle())
                .subscribe(new SingleObserver<List<ZDailyNewsSection>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onSuccess(List<ZDailyNewsSection> sections) {
                        if (isLoadMore) {
                            view.onLoadMoreZDailyNews(sections);
                        } else {
                            view.onLoadZDailyNews(sections);
                        }
                        view.onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isLoadMore) {
                            view.onLoadMoreError();
                        } else {
                            view.onError();
                        }
                    }
                });
    }

    @Override
    public void takeView(ZNewsContract.View view) {
        this.view = view;
    }

    @Override
    public void dropView() {
        if (view != null) {
            view = null;
        }
    }
}
